package com.liujun.spring4.demo.aop.ioc.base.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author liujun
 * @version 1.0
 * @date 2018-08-13 07:34:00
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {

    if (beanName.equals("car")) {
      Car car = (Car) bean;

      if (car.getColor() == null) {
        System.out.println("调用beanPostProcessor.postProcess BeforeInitialization().color为空，设置为黑色");
        car.setColor("黑色");
      }
    }

    return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    if ("car".equals(beanName)) {
      Car car = (Car) bean;
      if (car.getMaxSpeed() >= 200) {
        System.out.println("调用beanPostProcessor.postProcess AfterInitialiization.将maxSpeed设置为201");
        car.setMaxSpeed(201);
      }
    }

    return bean;
  }
}
